#include <stdio.h>
#include <string.h>
#include <shadow.h>
#include <crypt.h>
#include <unistd.h>

#define NAMESIZE	32

int main(void)
{
	char name[NAMESIZE] = { '\0' };
	char *pwd;
	struct spwd *shadow_info;
	char *crypt_pwd;

	printf("Username:");
	fgets(name, NAMESIZE, stdin);
	// 使用'\0'替换末尾'\n'
	*strchr(name, '\n') = '\0';

	pwd = getpass("Password:");

	shadow_info = getspnam(name);
	if (NULL == shadow_info) {
		perror("Error getting encrypted password");
		return 1;
	}

	crypt_pwd = crypt(pwd, shadow_info->sp_pwdp);

	if (strcmp(crypt_pwd, shadow_info->sp_pwdp) == 0) {
		printf("Login success.\n");
	} else {
		printf("Login failure.\n");
	}

	return 0;
}
